<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>

</head>

<body>
  <form action="" id="formdata">
    账号<input type="text" name="userName" /><br><br>
    密码<input type="password" name="password" /><br>
    <button type="submit">提交</button>
  </form>
  <script>
    const form = document.querySelector('#formdata')
    form.addEventListener('submit', function (e) {
      e.preventDefault()
      const fd = new FormData(this)
      const xhr = new XMLHttpRequest()
      xhr.open('POST', 'http://liulongbin.top:3006/api/formdata')
      // xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded')
      xhr.send(fd)
      xhr.addEventListener('readystatechange', function () {
        if (this.readyState === 4 && this.status === 200) {
          //获取服务器响应数据
          console.log(this.responseText)
          console.log(JSON.parse(this.responseText))
          console.log(fd)

        }
      })

    })
  </script>
</body>

</html>